﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BI.CF.IDAL;
using BI.CF.Model;
using System.Data.SqlClient;
using System.Data;
using BI.CF.PageList;

namespace BI.CF.DAL
{
     public  class MyAccountDAL:MyAccountIDAL
    { 
         /// <summary>
         /// 显示我的账户首页信息
         /// </summary>
         /// <param name="Userid"></param>
         /// <returns></returns>
        public MemPropertyModel MemProperty(int Userid)
        {
            SqlParameter[] paremeter ={
                                     new  SqlParameter("@Userid",SqlDbType.Int)
                                 };
            paremeter[0].Value = Userid;
            SqlDataReader read=DbHelperSQL.RunProcedure("PROC_MyAccountIndexSELECT",paremeter);
            MemPropertyModel Property = new MemPropertyModel();
            while (read.Read())
            {
                Property.AccountTotal = Convert.ToDecimal(read["AccountTotal"]);
                Property.AvailableBalance = Convert.ToDecimal(read["AvailableBalance"]);
                Property.FrozenTotal = Convert.ToDecimal(read["FrozenTotal"]);
                Property.IncomeSum = Convert.ToDecimal(read["IncomeSum"]);
            }
            return Property;
        }

         /// <summary>
         /// 用户执行充值
         /// </summary>
         /// <param name="RechargeMoney"></param>
         /// <param name="RechargeMode"></param>
         /// <param name="RechargeTime"></param>
         /// <param name="UserId"></param>
         /// <returns></returns>
        public int MemPropertyUpd(decimal RechargeMoney, string RechargeMode, DateTime RechargeTime, int UserId)
        {

            int i = 0;
            try
            {
                SqlParameter[] Parameter={
                                             new SqlParameter("@RechargeMoney",SqlDbType.Decimal),
                                             new SqlParameter("@RechargeMode",SqlDbType.VarChar),
                                         	 new SqlParameter("@RechargeTime",SqlDbType.DateTime),
                                             new SqlParameter("@UserId",SqlDbType.Int)
                                         };

                Parameter[0].Value = RechargeMoney;
                Parameter[1].Value = RechargeMode;
                Parameter[2].Value = RechargeTime;
                Parameter[3].Value = UserId;
                if (DbHelperSQL.RunProcedureReturnBool("PROC_MemPropertyUpdate", Parameter))
                {
                    i = 1;
                }
            }
            catch (Exception ee)
            {
                string Error = ee.Message;
            }
            return i;
        }
        /// <summary>
        ///  用户修改头像
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="ImageUrl"></param>
        /// <returns></returns>
        public int HeadImageUpdate(int UserId, string ImageUrl)
        {
            int i = 0;
            try
            {
                SqlParameter[] parameter ={
                                         new SqlParameter("@UserId",SqlDbType.Int),
                                        new SqlParameter("@HeadImageUrl",SqlDbType.VarChar)
                                         };
                parameter[0].Value = UserId;
                parameter[1].Value = ImageUrl;
                bool j=DbHelperSQL.RunProcedureReturnBool("PROC_UserHeadImageUpdate", parameter);
                if (j)
                {
                    return i = 1;
                }
            }
            catch (Exception ee)
            {
                string Error = ee.Message;
            }
            return i;
        }
          
         /// <summary>
         /// 显示充值记录
         /// </summary>
         /// <param name="Minmoney"></param>
         /// <param name="Maxmoney"></param>
         /// <param name="Mintime"></param>
         /// <param name="Maxtime"></param>
         /// <param name="RechargeMode"></param>
         /// <param name="UserId"></param>
         /// <param name="PageIndex"></param>
         /// <param name="PageSize"></param>
         /// <returns></returns>
        public Pager<RechargeRecordModel> ShowRechargeRecord(decimal Minmoney, decimal Maxmoney, DateTime Mintime, DateTime Maxtime, string RechargeMode, int UserId, int PageIndex, int PageSize)
        {
            List<RechargeRecordModel> list = new List<RechargeRecordModel>();
            Pager<RechargeRecordModel> Plist = new Pager<RechargeRecordModel>();
            try
            {
                SqlParameter[] Parameter ={
                                    new SqlParameter("@Minmoney",SqlDbType.Decimal),
                                     new SqlParameter("@Maxmoney",SqlDbType.Decimal),
                                      new SqlParameter("@Mintime",SqlDbType.DateTime),
                                       new SqlParameter("@Maxtime",SqlDbType.DateTime),
                                        new SqlParameter("@RechargeMode",SqlDbType.VarChar),
                                         new SqlParameter("@UserId",SqlDbType.Int),
                                          new SqlParameter("@PageIndex",SqlDbType.Int),
                                           new SqlParameter("@PageSize",SqlDbType.Int)
                                         };
                Parameter[0].Value=Minmoney;
                 Parameter[1].Value=Maxmoney;
                  Parameter[2].Value=Mintime;
                   Parameter[3].Value=Maxtime;
                    Parameter[4].Value=RechargeMode;
                     Parameter[5].Value=UserId;
                      Parameter[6].Value=PageIndex;
                       Parameter[7].Value = PageSize;
              int totalcount = 0;
              DataTable dt = DbHelperSQL.RunProcedure("PROC_RechargeRecordSELECT", Parameter,PageSize,PageIndex, out totalcount);
              for (int i = 0; i < dt.Rows.Count; i++)
	          {
	               RechargeRecordModel RechargeModel=new RechargeRecordModel();
                   RechargeModel.RechargeRecordId = Convert.ToInt32(dt.Rows[i]["RechargeRecordId"]);
                   RechargeModel.RechargeMode=  dt.Rows[i]["RechargeMode"].ToString();
                   RechargeModel.RechargeMoney=Convert.ToDecimal(dt.Rows[i]["RechargeMoney"]);
                   RechargeModel.RechargeRecordId=Convert.ToInt32(dt.Rows[i]["RechargeRecordId"]);
                   RechargeModel.RechargeTime=Convert.ToDateTime(dt.Rows[i]["RechargeTime"]);
                   RechargeModel.UserName=dt.Rows[i]["UserName"].ToString();
                   RechargeModel.UserrId = Convert.ToInt32(dt.Rows[i]["UserId"]);
                   list.Add(RechargeModel);
	          }
              Plist.Data = list;
              Plist.PageCount = totalcount / PageSize;
              if (totalcount%PageSize!=0)
              {
                  Plist.PageCount = Plist.PageCount+ 1;
              }
              Plist.PageIndex = PageIndex;
              Plist.PageSize = PageSize;
              
            }
            catch (Exception ee)
            {
                string Error = ee.Message;
            }
            return Plist;
        }
    }
}
